package com.hpe.aruba.CPPM.utils;

import android.util.Log;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.hpe.aruba.CPPM.interfaces.JsonInterface;
import com.hpe.aruba.CPPM.interfaces.StringInterface;
import com.hpe.aruba.CPPM.interfaces.VolleyErrorInterface;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class WebUtils {

    public static void delete(RequestQueue queue, String url, Map<String, String> header, StringInterface onSuccess, StringInterface onError) {
        StringRequest stringRequest = new StringRequest(Request.Method.DELETE, url,
                response -> {
                    VolleyLog.d("WEB_OP", response);
                    onSuccess.apply(response);
                }, error -> {
            VolleyLog.e("Error: ", error.getMessage());
            onError.apply(error.getMessage());
        }) {
            @Override
            public Map<String, String> getHeaders() {
                Map<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "application/json");
                headers.put("charset", "utf-8");
                headers.putAll(header);
                return headers;
            }
        };

        queue.add(stringRequest);
    }

    public static void get(RequestQueue queue, String
            url, Map<String, String> header, StringInterface onSuccess, StringInterface onError) {
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                response -> {
                    VolleyLog.d("WEB_OP", response);
                    onSuccess.apply(response);
                }, error -> {
            VolleyLog.e("Error: ", error.getMessage());
            onError.apply(error.getMessage());
        }) {
            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                int mStatusCode = response.statusCode;
                VolleyLog.d("STATUS CODE: " + mStatusCode);
                if (mStatusCode == 404) {
                    onError.apply("" + mStatusCode);
                }
                return super.parseNetworkResponse(response);
            }
        };
        queue.add(stringRequest);
    }

    public static void getJson(RequestQueue queue, String
            url, Map<String, String> header, JsonInterface onSuccess, StringInterface onError) {
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
                response -> {
                    VolleyLog.d("WEB_OP", response);
                    onSuccess.apply(response);
                }, error -> {
            VolleyLog.e("Error: ", error.getMessage());
            onError.apply(error.getMessage());
        }) {
            @Override
            public Map<String, String> getHeaders() {
                Map<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "application/json");
                headers.put("charset", "utf-8");
                headers.putAll(header);
                return headers;
            }
        };
        queue.add(jsonObjectRequest);
    }

    public static void patch(RequestQueue queue, String
            url, JSONObject patch, Map<String, String> header, JsonInterface onSuccess, StringInterface onError) {
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.PATCH, url, patch,
                response -> {
                    VolleyLog.d("WEB_OP", response);
                    onSuccess.apply(response);
                }, error -> {
            VolleyLog.e("Error: ", error.getMessage());
            onError.apply(error.getMessage());
        }) {
            @Override
            public Map<String, String> getHeaders() {
                Map<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "application/json");
                headers.put("charset", "utf-8");
                headers.putAll(header);
                return headers;
            }
        };
        queue.add(jsonObjectRequest);
    }

    public static void post(RequestQueue queue, String
            url, Map<String, String> header, JSONObject data, StringInterface
                                    onSuccess, StringInterface onError) {

        StringRequest jsonObjectRequest = new StringRequest(Request.Method.POST, url,
                onSuccess::apply, error -> error.printStackTrace()) {
            @Override
            public Map<String, String> getHeaders() {
                Map headers = new HashMap();
                headers.put("Content-Type", "application/json");
                headers.put("charset", "utf-8");
                headers.putAll(header);
                return headers;
            }

            @Override
            public byte[] getBody() {
                return data.toString().getBytes();
            }

            @Override
            public String getBodyContentType() {

                return "application/json; charset=UTF-8";
            }
        };

//        jsonObjectRequest.setTag(REQ_TAG);
        queue.add(jsonObjectRequest);
    }

    public static void postJson(RequestQueue queue, String
            url, Map<String, String> header, JSONObject data, JsonInterface
                                        onSuccess, VolleyErrorInterface onError) {

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, data,
                onSuccess::apply, onError::apply) {
            @Override
            public Map<String, String> getHeaders() {
                Map<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "application/json");
                headers.putAll(header);
                return headers;
            }
        };
//        jsonObjectRequest.setTag(REQ_TAG);
        queue.add(jsonObjectRequest);
    }
}
